package com.njust.RateMyTray_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njust.RateMyTray_backend.entity.Notification;
import com.njust.RateMyTray_backend.mapper.NotificationMapper;
import com.njust.RateMyTray_backend.service.NotificationService;
import com.njust.RateMyTray_backend.vo.NotificationVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {
    @Override
    public List<NotificationVO> getNotificationsForUser(Long userId) {
        LambdaQueryWrapper<Notification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notification::getUserId, userId)
                .orderByDesc(Notification::getCreatedAt);

        List<Notification> notifications = this.list(queryWrapper);

        return notifications.stream().map(notification -> NotificationVO.builder()
                .id(notification.getId())
                .type(notification.getType())
                .content(notification.getContent())
                .read(notification.getIsRead())
                .timestamp(notification.getCreatedAt())
                .build()).collect(Collectors.toList());
    }

    @Override
    public void markAllNotificationsAsRead(Long userId) {
        LambdaUpdateWrapper<Notification> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Notification::getUserId, userId)
                .eq(Notification::getIsRead, false)
                .set(Notification::getIsRead, true);
        
        this.update(updateWrapper);
    }
} 